package com.haoze.common.utils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class ConcurrentDateUtil {

    private static String FORMAT = "yyyy-MM-dd HH:mm:ss";

    private static ThreadLocal<DateFormat> threadLocal = ThreadLocal.withInitial(() -> new SimpleDateFormat(FORMAT));

    public static Date parse(String dateStr, String format) throws ParseException {
        FORMAT = format;
        return threadLocal.get().parse(dateStr);
    }

    public static Date parse(String dateStr) throws ParseException {
        return threadLocal.get().parse(dateStr);
    }

    public static String format(Date date, String format) {
        FORMAT = format;
        return threadLocal.get().format(date);
    }

    public static String format(Date date) {
        return threadLocal.get().format(date);
    }
}
